<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>3、ECMAScript基础之闭包复习</title>
</head>
<body>
    <script>
        ;(function(){
            function test(){
                var num = 0; // 私有变量
                function add(){
                    num++;
                    console.log(num);
                }
                return add;
            }    

            var add = test();
            add();
            add();
            add();
        })()

        ;(function(){
            function test(){
                var num = 0; // 私有变量
                var compute = {
                    add: function(){
                        num++;
                        console.log(num);
                    },
                    minus: function(){
                        num--;
                        console.log(num);
                    }
                }
                return compute;
            }

            var compute = new test();
            compute.add()
            compute.add()
            compute.add()
            compute.minus()
        })()

        ;(function(){
            function Compute(){
                this.num = 10;
                this.add = function(){
                    this.num++;
                    console.log(this.num);
                }
                this.minus = function(){
                    this.num--;
                    console.log(this.num);
                }

                // return []; // 不能return引用值
            }

            var compute = new Compute();

            compute.add()
            compute.add()
            compute.add()
            compute.add()
            compute.minus()
        })()
    </script>
</body>
</html>